#!/usr/bin/perl
#lvcreate.cgi
require './lvm-lib.pl';
&ReadParse();
my ($err_msg, $found, $vgname, @pvinfo, @vginfo, @lvinfo, $v, $vg);
$vgname = $in{'vg'};
if(!$vgname){
	$err_msg = $text{'noselectvg'};
	goto ERROR_OUT;
}
@vginfo = get_vg_info();
@lvinfo = get_lv_info();
@pvinfo = get_pv_info();
$found = 0;
foreach $v (@vginfo) {
	if($v->{'vg'} eq $vgname){
		$vg = $v;
		$found = 1;
	}
}
if(!$found){
	$err_msg = $text{'err_vgnofound'};
	goto ERROR_OUT;
}
&header($text{'title_vgconfig'}, "", "vgextend", 0, 1);
print "<form name=form_vgconfig action=index.cgi method=post>\n";
print "<table class=mode1 width=80%>";
print "<tr><td width=200><b>$text{'index_vg'}</b></td><td><b>$vg->{'vg'}</b></td></tr>\n";
print "<tr><td width=200><b>$text{'index_vg_free'}</b></td><td>$vg->{'free'}</td></tr>\n";
my @vgpvs = get_pvs_of_vg($vg, @pvinfo);
my $vp;
my $j = 0;
my $vpnum = @vgpvs;
my $disabled="";
if($vpnum == 1){
	$disabled="disabled=true";
}
print "<tr><td width=200><input type=radio name=adrm checked value=0 onclick='SetReduceVg($vpnum)'><b>$text{'radio_removepv'}</b></td>";
print "<td><select name=vgpv class=drop>";
foreach $vp (@vgpvs) {
	if ($j == 0) {
		print "<option selected value=$vp>$vp";
	}
	else {
		print "<option value=$vp>$vp";
	}
	$j++;
}
print "</select>";
if($vpnum == 1){
        print "<i>".$text{'warning_no_remove_vg'}."</i>";
}
print "</td></tr>\n";

my @freepvs = get_free_pv(@pvinfo);
my $fp;
my $i = 0;
my $fpnum = @freepvs;
print "<tr><td width=200><input type=radio name=adrm value=1 onclick='SetExtendVg($fpnum)'><b>$text{'radio_addpv'}</b></td>";

print "<td><select name=freepv class=drop disabled=true>";
foreach $fp (@freepvs) {
	if ($i == 0) { 
		print "<option selected value=$fp->{'pv'}>$fp->{'pv'}";
	}
	else {
		print "<option value=$fp->{'pv'}>".$fp->{'pv'};
	}
	$i++;
}
print "</select>";
if($fpnum == 0){
	print "<i>".$text{'warning_no_extend_vg'}."</i>";
}
print "</td></tr>\n";

#print "<tr><td width=200><b>Logical Volume Size</b></td>";
#print "<td><input name=lvsize class=text size=10> GB</td></tr>\n";
print "</table>\n";

print "<table border=0 class=mode1>\n";
print "<tr><td>";
print "<input type=submit name=config class=button1 value=$text{'bt_submit'} onclick='return SetEditValue(this)' $disabled>\n"; 
print "</td>";
print "<td><input type=submit name=cancel class=button1 value=$text{'bt_cancel'}></td>";
print "</tr>\n";
print "</table>\n";
print "<input type=hidden name=vgname value=$vg->{'vg'}>";
print <<EOF;
<script>
function SetEditValue(obj)
{
        if (obj == document.form_vgconfig.config) {
                document.form_vgconfig.action = 'act_vgconfig.cgi';
        }
}

function SetReduceVg(m)
{
		if (m == 1){
			document.form_vgconfig.config.disabled=true;
		}
		else
		{
			document.form_vgconfig.config.disabled=false;
		}

		document.form_vgconfig.freepv.disabled = true;
		document.form_vgconfig.vgpv.disabled = false;
}
function SetExtendVg(n)
{
		if (n == 0){
			document.form_vgconfig.config.disabled=true;
		}
		else
		{
			document.form_vgconfig.config.disabled=false;
		} 
		document.form_vgconfig.freepv.disabled = false;
		document.form_vgconfig.vgpv.disabled = true;
}
</script>
EOF
print "</form>\n";
&footer("/lvm", $text{'index'});
return;
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
